<?php
/*
 * The main template file.
 *
 * @package WordPress
 * @subpackage urbanmeandering
 */

global $wp_query, $wpdb; 
 
$keyword = esc_html( get_search_query() );
$slug_title = 'search';
$archive_title = 'Search results for "' . $keyword .'"';

$limit = $wp_query->query_vars['posts_per_page'];
$active_page = isset( $_GET['page'] ) && $_GET['page'] ? (int)$_GET['page'] : 1;
$offset = ( $active_page - 1 ) *  $limit;
$results = $wpdb->get_results( " SELECT SQL_CALC_FOUND_ROWS * FROM $wpdb->posts  
																				WHERE 1=1  AND ((( `post_title` LIKE '%" . $keyword . "') OR
																					( `post_content` LIKE '%" . $keyword . "%')))  AND 
																					( `post_password` = '')  AND 
																					`post_type` IN ('post', 'page', 'food', 'travel-activity', 'entertainment', 'advocacy-charity', 'giveaways' ) AND 
																					( `post_status` = 'publish')  
																					ORDER BY `post_date` DESC LIMIT $limit OFFSET $offset ");
$total = $wpdb->get_var( 'SELECT FOUND_ROWS()' );																					
get_header(); ?>
<div id="main" class="group">
	<?php um_breadcrumbs( $archive_title ); ?>
	
	<section id="search-posts" class="archive posts-list-overview content">
		<h1 class="page-label"><span class="label"><?php echo $slug_title; ?></span><span class="right">&nbsp;</span></h1>
		<div class="group">
			<?php
			if( $results )
			{
				?>
				<h2 id="search-title"><?php echo $archive_title; ?></h2>
				<?php 
				foreach ( $results as $post )
				{ 
					$excerpt = $post->post_excerpt ? $post->post_excerpt : $post->post_content;
					?>
						<article>
								<a href="<?php echo get_permalink( $post->ID ); ?>"><?php echo get_the_post_thumbnail( $post->ID,'302x170' ); ?></a>
							<div class="text">
								<a href="<?php echo get_permalink( $post->ID ); ?>"><h2 class="entry-title"><?php echo $post->post_title; ?></h2></a>
								<p class="entry-desc"><time datetime="<?php echo $post->post_date; ?>"><?php echo date( get_option( 'date_format' ), strtotime( $post->post_date ) ); ?></time> - <?php echo wp_trim_words( $excerpt, 15 ); ?></p>						
							</div>
							<span class="comment-count">
									<a href=" <?php comments_link(); ?>"><?php comments_number( 'No comments yet.', '1 Comment', '% Comments' ); ?> </a>
								</span>
						</article>
					<?php 
				} 
			}
			else
			{
				?>
					<h2 id="search-title">No search results found for "<?php echo $keyword; ?>".</h2>
				<?php
			}
			?>
		</div>
		<?php 
		if( $results )
		{
			search_page_navigation( $total, $limit, $active_page ); 
		}
		?>
	</section>
	<?php get_sidebar(); ?>
</div>
<?php get_footer(); ?>